function checkExist(objMain,objMultiList) // function cheching where id already existing or not
{


	//var district=document.getElementById('district1').value;
	//var obj=document.getElementById('multi');
	for (var i = 0;i<objMultiList.length;i++) 
	{
		if(objMain.value==objMultiList.options[i].value)
		{
			alert("The chosen district is already added.");
			return false;
		}
	}
	return true;
}

function createDropdown(data,objMain,objMultiList) //function will be fired when click on add button
{
	//alert(objMultiList.value);
	//for checing If the district has been chosen already....
	if(checkExist(objMain,objMultiList))
	{
		
		var selIndex = objMain.selectedIndex;
		var selText=objMain.options[selIndex].text;
		//var district=document.getElementById('district1').value;
		//var obj=document.getElementById('multi');
		//var obj1=document.getElementById('portfolio');
		if(objMain.value == "" ) 
		{
			alert("Please select any district ");
			//return false;
		}

		opt = document.createElement("OPTION");
		//alert(opt);
		opt.value = objMain.value;
		opt.text=selText;
		opt.visible=true;
		//opt.selected=true;
		//objMultiList.add(opt,null);
		objMultiList.add(opt,null);	
			
		if(data=="block")
		{
		changeBlock(data);
		}
		
		if(data=="panchayat")
		{
		changePanchayat(data);
		}
		if(data=="village")
		{
		changeVillage(data);
		}
		if(data=="habit")
		{
		changeHabit(data);
		}
		getvariable();
		
	}

}





function removeOption(data,valmulti) //function will be fired when click on remove button
{
	//alert(valmulti.selectedIndex);
	if(valmulti.selectedIndex<0)
	{
		alert ("Please select any option ");
		//return false;
	}
	else 
	{
		var lengtharr=valmulti.length;
		//alert(lengtharr);
		var arrdistrict = new Array(lengtharr);
		for (var i=valmulti.options.length-1;i>=0;i--) 
		{
			arrdistrict[i]=valmulti.options[i].value;
			if(valmulti.options[i].selected)
			{
				//alert(valmulti.options[i].selected);
				valmulti.remove(i);
		if(data=="block")
		{
		changeBlock(data);
		}
		if(data=="tehsil")
		{
		changeTehsil(data);
		}
		if(data=="panchayat")
		{
		changePanchayat(data);
		}
		if(data=="village")
		{
		changeVillage(data);
		}
		if(data=="habit")
		{
		changeHabit(data);
		}
		getvariable();
			}
		
		}
	 
	}
}




// JavaScript Document
function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};
		
function changeDistrict(district,val) 
{
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('district').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+district+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}
window.onLoad=changeDistrict('district',-1);

function changeBlock(block) 
{
	
	var val="";
    var obj=document.getElementById('multiDist');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('blockLoader').innerHTML="<img src='images/ajax-loader.gif' alt='image' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('blockLoader').innerHTML=" ";
				document.getElementById('block').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+block+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	changeTehsil("tehsil") 
	
}

function changeTehsil(tehsil) 
{
	var val="";
    var obj=document.getElementById('multiDist');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	

	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('tehsilLoader').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('tehsilLoader').innerHTML=" "; 
				document.getElementById('tehsil').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+tehsil+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	
}
function getvariable() {
   var valTehsil="";
    var obj=document.getElementById('multiTehsil');
	
	for (var i = 0;i<obj.length;i++) 
	{
		 
			 valTehsil = valTehsil+" " +obj.options[i].value;
		    
			document.questionnaire1.mulTehsil.value = valTehsil;
			 
	}
}

function changePanchayat(panchayat) 
{
	var val="";
    var obj=document.getElementById('multiBlock');
	for (var i = 0;i<obj.length;i++) 
	{
			 val = val + " " + obj.options[i].value;
	}
	//alert(val);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('panchayatLoader').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('panchayatLoader').innerHTML=" "; 
				document.getElementById('panchayat').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+panchayat+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changeVillage(village) 
{
	
	var val="";
    var obj=document.getElementById('multiPanchayat');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('villageLoader').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('villageLoader').innerHTML=" ";
				document.getElementById('village').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+village+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	
}



function changeHabit(habit) 
{
	
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('habit').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+habit+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	changePopulation("population_benefited");
	changePrePopulation("present_population");
	changeCensusCode("cvc");
}

function changeCensusCode(cvc) 
{
	
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('cvc').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+cvc+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changeFormId(form,form_id,val) 
{
	//var dis_name = form.district.options[form.district.selectedIndex].text;
	//alert(dis_name);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
	   //alert(req.readyState);
		if (req.readyState == 4)
		{
			//alert(req.status);
			if (req.status == 200) 
			{
				document.getElementById('form_id').innerHTML = req.responseText; 
			}
		}
	};
	var dis_name = form.district.options[form.district.selectedIndex].text;
	req.open("post", "dropdown_rwss.php?dist="+dis_name+"&data="+form_id+"&val="+val);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changePopulation(population_benefited) 
{
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('population_benefited').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+population_benefited+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changePrePopulation(present_population) 
{
	
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('present_population').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+present_population+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function change_factor(factor, val) 
{
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('factor').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+factor+"&val="+val);
	//alert("dropdown_q2.php?data="+factor+"&val="+val);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}


function changeHP(data) 
{
	var num=document.getElementById('totalRow').value;
	//alert(num);
    var hpNo=new Array();
	var sum=0;
	for(var i=1;i<=num;i++)
	{
		if(document.getElementById('HP_no'+i).value!="")
		{
		 hpNo[i]=document.getElementById('HP_no'+i).value;
		 sum=sum+parseInt(hpNo[i]);
		}
		
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('hpno').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	//alert("questionnaire_q1_HPSP.php?data="+data+"&val="+val+"&obj="+obj);
	req.open("post", "questionnaire_q1_HPSP.php?data="+data+"&val="+sum); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changeSP(data) 
{
	//alert(data);
	var num=document.getElementById('totalRow').value;
	//alert(num);
    var spNo=new Array();
	var sum=0;
	for(var i=1;i<=num;i++)
	{
		if(document.getElementById('SP_no'+i).value!="")
		{
		 spNo[i]=document.getElementById('SP_no'+i).value;
		 sum=sum+parseInt(spNo[i]);
		 }
	}

	var req = Inint_AJAX();
	
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				//alert("hi");
				document.getElementById('spno').innerHTML=req.responseText; 
						//	alert(req.responseText);
			} 
		}
	};
	//alert("questionnaire_q1_HPSP.php?data="+data+"&val="+sum);
	req.open("post", "questionnaire_q1_HPSP.php?data="+data+"&val="+sum); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}



function changePopulationHABITWISE(valobj,population_benefited,val_habit,val,obj) 
{
		
	if(document.getElementById('coverN'+obj).checked==true)
	  {
	   var flage=1;
	  }else{
		 var flage=0; 
		  }
		
	var population=document.getElementById("population_benefited1").value;
	//alert(population);
	//alert(val_habit);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('population_benefited').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+population_benefited+"&val_habit="+val_habit+"&val="+val+"&new_population="+population+"&valobj="+valobj+"&flage="+flage); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}
function changePopulationHABITWISENC(valobj,population_benefited,val_habit,val,obj) 
{
	
	if(document.getElementById('cover'+obj).checked==true)
	  {
	   var flage=1;
	  }else{
		 var flage=0; 
		  }
		
	var population=document.getElementById("population_benefited1").value;
	//alert(population);
	//alert(val_habit);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('population_benefited').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_rwss.php?data="+population_benefited+"&val_habit="+val_habit+"&val="+val+"&new_population="+population+"&valobj="+valobj+"&flage="+flage); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}


function changePopulationHABITWISENT(population_benefited,val_habit,val,obj) 
{
	
	if(document.getElementById('coverN'+obj).checked==true)
	  {
	   var flage=1;
	  }else{
		 var flage=0; 
		  }
	var population=document.getElementById("population_benefited1").value;
	//alert(population);
	//alert(val_habit);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('population_benefited').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	//alert("dropdown_rwss.php?data="+population_benefited+"&val_habit="+val_habit+"&val="+val+"&new_population="+population+"&flage="+flage);
	req.open("post", "dropdown_rwss.php?data="+population_benefited+"&val_habit="+val_habit+"&val="+val+"&new_population="+population+"&flage="+flage); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function setdisOnly(valobj,obj)
{ 
    if(document.getElementById('statusN'+obj).checked==true)
	{
		document.getElementById('statusN'+obj).disabled=true;
		document.getElementById('cover'+obj).disabled=false;
	}
	if(document.getElementById('status'+obj).checked==true)
	{
		document.getElementById('cover'+obj).disabled=false;
	}
	else
	{
		document.getElementById('cover'+obj).disabled=true;
	}
	document.getElementById('coverN'+obj).checked=true;
	document.getElementById('coverN'+obj).disabled=true;
}
function setEnableOnly(valobj,obj)
{ 
	document.getElementById('cover'+obj).disabled=false;
	if(document.getElementById('cover'+obj).checked==true)
	{
		document.getElementById('coverN'+obj).disabled=false;
		document.getElementById('cover'+obj).disabled=true;
	}
	if(document.getElementById('status'+obj).checked==true)
	{
		document.getElementById('statusN'+obj).disabled=false;
	}
}

